<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\wxapp\controller;

use api\wxapp\controller\ApiBaseController;
use think\Db;
use api\wxapp\model\User;
use api\wxapp\model\Staff;
use api\wxapp\model\Shop;
use api\wxapp\model\Order;
use api\wxapp\model\Goods;
use api\wxapp\model\OrderList;
class StaffController extends ApiBaseController
{
    //判断 束式员工的权限
    public function initialize()
    {
        parent::initialize();
        $params = $this->getParam();
        $this->checkuid($params);
        $find = Staff::where('uid',$params['uid'])->find();
        if(!$find){
            $this->error('没有权限!!!!!');
        }
    }
    public function index()
    {
        $params = $this->getParam();
        $info = Staff::where('uid',$params['uid'])->field('depart')->find();
        $find = User::get($params['uid']);
        $info['img'] = $find['img'];
        $info['nickname'] = $find['nickname'];
        $this->success('请求成功!',$info);
    }
    public function rcode()
    {
        $params = $this->getParam();
        //查询当前店员是否生成二维码
        $info = Staff::where('uid',$params['uid'])->find();
        if($info['rcode']){
            $this->success('请求成功!',$info['rcode']);
        }else{
            $rcode = new WxRcodeController;
            $data = json_encode(array("path"=>"pages/index/index?staff_id=".$info['id']."&type=0","width"=> 50));
            $res = $rcode->index($data,$params['uid']);
            Staff::where('id',$info['id'])->update(['rcode'=>$res]);
            $this->success('请求成功!',$res);
        }
    }
    //
    public function shop()
    {
        $params = $this->getParam();
        !isset($params['staff_id']) ? $this->error('参数缺失!!!!') : '';
        $list = Shop::where('staff_id',$params['staff_id'])->select();
        $this->success('请求成功!',$list);
    }
    //
    public function sales()
    {
        $params = $this->getParam();
        $info['num'] = OrderList::where('shop_id',0)->SUM('num');
        $info['money'] = Order::where('shop_id',0)->SUM('pay_price');
        $this->success('请求成功!',$info);
    }
    public function goods()
    {
        $params = $this->getParam();
        $page = isset($params['page']) ? $params['page'] : 1;
        $limit = ($page-1)*10;
        $list = Goods::field('id,name,img,price')->limit($limit,10)->select()
            ->each(function($item,$key){
                $item['total'] = OrderList::where(['goods_id'=>$item['id'],'shop_id'=>0])->SUM('num');
            });
        $this->success('请求成功!',$list);
    }
}
